window._RHTJ = {

    _MAINLABEL:null,

    _ROLE:null,

    _TJRHOBJ:null,

    _TJRHOBJARR:null,

    _TJRHOBJINDEX:0,

    _TIPSMSG:"1.推荐融合是根据已经解锁的图鉴筛选出可能融合方案\n2.推荐融合只会图鉴评级高于角色的方案,如果角色评级高于SS则不会再有推荐\n3.解锁图鉴可以增加推荐融合方案\n4.如果不满意推荐融合方案可以自行前往融合商人进行融合",

    showRHTj:function(parentNode,param){

        if(!param||!param.role)
            return ;

        _RHTJ._ROLE = param.role;
        _RHTJ._TJRHOBJ = null;
        _RHTJ._TJRHOBJARR = [];
        _RHTJ._TJRHOBJINDEX = 0;
        if(!_RHTJ.getTjRhObj()){
            _TIPS.showMessage("暂时没有可推荐的融合方案,建议多去野外抓怪解锁地图");
            return null;
        }

        var uiObj = _GRM.getNodeObj('rhtj');

        _RHTJ._MAINLABEL = uiObj.node;

        if(uiObj.isnew){
            _RHTJ.initButton();
        }

        parentNode.addChild(_RHTJ._MAINLABEL);

        _RHTJ.refresh();

        return _RHTJ._MAINLABEL;
    },

    initButton:function(){
        var mainLabel = _RHTJ._MAINLABEL;
        var maskNode = mainLabel.getChildByName('mask');
        maskNode.on(cc.Node.EventType.TOUCH_START, function(){
            _RHTJ.closeUi();
        }, maskNode);


        var yjrhBtn = mainLabel.getChildByName('explain').getChildByName('rhbtn');
        yjrhBtn.on(cc.Node.EventType.TOUCH_START, function(){
            console.log('一件融合');
            _RHTJ.yjrh();
        }, yjrhBtn);


        var introbtn = mainLabel.getChildByName('explain').getChildByName('introbtn');
        introbtn.on(cc.Node.EventType.TOUCH_START, function(){
            console.log('一件融合说明');
            _TIPS.showMessage(_RHTJ._TIPSMSG);
        }, introbtn);


        var changebtn = mainLabel.getChildByName('explain').getChildByName('changebtn');
        changebtn.on(cc.Node.EventType.TOUCH_START, function(){
            console.log('交换');
            if(_RHTJ._TJRHOBJARR&&_RHTJ._TJRHOBJARR.length>1){
                if(_RHTJ._TJRHOBJINDEX<(_RHTJ._TJRHOBJARR.length-1)){
                    _RHTJ._TJRHOBJINDEX++;
                }else{
                    _RHTJ._TJRHOBJINDEX=0;
                }
                _RHTJ._TJRHOBJ = _RHTJ._TJRHOBJARR[_RHTJ._TJRHOBJINDEX];
                _RHTJ.refresh();
            }
        }, changebtn);
    },

    getTjMap:function(race){

        var tjmap = {};
        for(var key in _NPC){
            var npcConf = _NPC[key];
            if(
                npcConf.race==race
                &&_GAMEDATAHELPER.hasNpcTj(key)
            ){
                var maxxw = _RHM.getMaxXwObj(npcConf.npcxw);
                if(maxxw){
                    var xw = maxxw.xw;
                    var level = maxxw.level;
                    if(!tjmap[xw]){
                        tjmap[xw]={lv:level,npc:key};
                    }else{
                        if(_RHM.getLevelToNmb(level)<_RHM.getLevelToNmb(tjmap[xw].lv)){
                            tjmap[xw]={lv:level,npc:key};
                        }
                    }
                }

            }
        }

        return tjmap;
    },

    getTjRhObj:function(){
        var npcconf = _NPC[_RHTJ._ROLE.npctype];
        var race = npcconf.race;
        var level = npcconf.level;
        var xwlevel = _RHTJ._ROLE.xwlevel?_RHTJ._ROLE.xwlevel:npcconf.npcxw;
        var tjmap = _RHTJ.getTjMap(race);
        console.log(tjmap);
        var tjrhresult = _RHM.getTjrhByNpc(race,level,xwlevel,tjmap);
        console.log(tjrhresult);

        if(tjrhresult&&tjrhresult.length>0){
            _RHTJ._TJRHOBJARR = tjrhresult;
            _RHTJ._TJRHOBJINDEX = 0;
            _RHTJ._TJRHOBJ = tjrhresult[0];
            return true;
        }else{
            return false;
        }
        
    },

    refresh:function(){
        console.log(_RHTJ._ROLE,_RHTJ._TJRHOBJ);
       
        _RHTJ.refreshself();
        _RHTJ.refreshTarget();
        _RHTJ.refreshCailiao();
        _RHTJ.refreshTips();
       
    },

    refreshNpcInfo:function(imgNode,xwdetailNode,npc,npcxw){
        var npcConf = _NPC[npc];
        if(npcxw==null){
            npcxw=npcConf.npcxw;
        }
    },

    refreshImgNode:function(npctypeConfig,node){
        var npcjob = npctypeConfig.job;
        var npcrace = npctypeConfig.race;
        var npcqua = npctypeConfig.level;

        node.getChildByName('img').getComponent(cc.Sprite).spriteFrame = _GRM.getFaceFrame(npctypeConfig.face);
        var nameNode = node.getChildByName('name');
        var ftypeNode = node.getChildByName('wuxin');
        var ftypeStrNode = ftypeNode.getChildByName('label');
        ftypeNode.color = new cc.color(window._CS.ftype[npctypeConfig.ftype].color);
        ftypeStrNode.getComponent(cc.Label).string = npctypeConfig.ftype;
        var jobNode = node.getChildByName('job');
        var jobStrNode = jobNode.getChildByName('ftypeStr');
        jobNode.color = new cc.color(window._JOB[npcjob].color);
        jobStrNode.getComponent(cc.Label).string = window._JOB[npcjob].sname;
        var zhongNode = node.getChildByName('zhong');
        var zhongStrNode = zhongNode.getChildByName('ftypeStr');
        zhongNode.color = new cc.color(window._RACE[npcrace].color);
        zhongStrNode.getComponent(cc.Label).string = window._RACE[npcrace].name;
        var quaNode = node.getChildByName('level');
        var quaStrNode = quaNode.getChildByName('ftypeStr');
        quaNode.color = new cc.color(window._CS.qua[npcqua].color);
        quaStrNode.getComponent(cc.Label).string = npcqua;

        nameNode.getComponent(cc.Label).string = npctypeConfig.name;
    },  

    refreshself:function(){
        var mainLabel = _RHTJ._MAINLABEL;
        var selfNode = mainLabel.getChildByName('explain').getChildByName('self');
        var imgNode = selfNode.getChildByName('goods').getChildByName('imgs');
        var xwdetailNode = selfNode.getChildByName('xwdetail');
        
        _RHTJ.refreshImgNode(_NPC[_RHTJ._ROLE.npctype],imgNode);

        var detail = _RHTJ._TJRHOBJ.detail.detail;
        var detailmsg = "";
        for(var xw in detail){
            var xwname = _XW[xw].label;
            var xwlevel = detail[xw][1];
            if(xwlevel){
                detailmsg+=xwname+":"+xwlevel+" ";
            }
        }
        xwdetailNode.getComponent(cc.Label).string = detailmsg;

    },

    refreshTarget:function(){
        var mainLabel = _RHTJ._MAINLABEL;
        var targetNode = mainLabel.getChildByName('explain').getChildByName('target');
        var imgNode = targetNode.getChildByName('goods').getChildByName('imgs');

        _RHTJ.refreshImgNode(_NPC[_RHTJ._ROLE.npctype],imgNode);
        var xwdetailNode = targetNode.getChildByName('xwdetail');

        var targetConf = _NPC[_RHTJ._TJRHOBJ.role];
        _RHTJ.refreshImgNode(targetConf,imgNode);

        var detail = _RHTJ._TJRHOBJ.detail.detail;
        var detailmsg = "";
        for(var xw in detail){
            var xwname = _XW[xw].label;
            var xwlevel = detail[xw][0];
            if(xwlevel){
                detailmsg+=xwname+":"+xwlevel+" ";
            }
        }
        xwdetailNode.getComponent(cc.Label).string = detailmsg;
    },

    refreshTips:function(){

        var mainLabel = _RHTJ._MAINLABEL;
        var targetNode = mainLabel.getChildByName('explain').getChildByName('rhnmb');
        var neednmb = _RHTJ._TJRHOBJ.detail.rhnmb;
        targetNode.getComponent(cc.Label).string = '需要融合丹药('+_RHTJ.getrhdnmb()+'/'+neednmb+')';

        var changebtn = mainLabel.getChildByName('explain').getChildByName('changebtn');
        changebtn.getChildByName('Background').getChildByName('Label')
        .getComponent(cc.Label).string = "更换("+_RHTJ._TJRHOBJARR.length+")";
    },

    getrhdnmb:function(){
        return _GAMEDATAHELPER.getGoodsNmb(window.gameData.specilItem,'融合丹');
    },

    getNpcNmb:function(npctype){
        var nmb = 0;
        for(var i=0;i<window.gameData.playerTeamBack.length;i++){
            var role = window.gameData.playerTeamBack[i];
            if(role.npctype==npctype){
                nmb++;
            }
        }
        return nmb;
    },

    refreshCailiao:function(){
        var mainLabel = _RHTJ._MAINLABEL;
        var detail = _RHTJ._TJRHOBJ.detail.detail;
        var cailiaoNodes = mainLabel.getChildByName('explain').getChildByName('cailiao').getChildren();
        for(var i=0;i<cailiaoNodes.length;i++){
            cailiaoNodes[i].active = false;
        }
        var nodeindex =0;
        for(var key in detail){
            if(detail[key][2]){
                var xwname = _XW[key].label;
                var xwlevel = detail[key][2].lv;
                var npc = detail[key][2].npc;
                var neednmb = detail[key][2].nmb;
                var hasnmb = _RHTJ.getNpcNmb(npc);
                cailiaoNodes[nodeindex].active = true;
                var imgnode = cailiaoNodes[nodeindex].getChildByName('imgs');
                var detailnode = cailiaoNodes[nodeindex].getChildByName('words').getChildByName('title');
                
                _RHTJ.refreshImgNode(_NPC[npc],imgnode);
                detailnode.getComponent(cc.Label).string = xwname+xwlevel+"("+hasnmb+"/"+neednmb+")";
                nodeindex++;
            }
        }
    },

    closeUi:function(){
        if(_RHTJ._MAINLABEL){
            _GRM.putNode(_RHTJ._MAINLABEL);
            _RHTJ._MAINLABEL = null;
        }
    },

    yjrh:function(){
        if(_RHTJ.canRh()){

            console.log('可以融合');
            var rhdnmb = _RHTJ._TJRHOBJ.detail.rhnmb;
            var npcarr = _RHTJ._TJRHOBJ.detail.detail;
            var targetNpc = _RHTJ._TJRHOBJ.role;

            _GAMEDATAHELPER.reduceSomeItemByName('融合丹',rhdnmb);
            for(var key in npcarr){
                var npcinfo = npcarr[key][2];
                if(npcinfo){
                    var npc = npcinfo.npc;
                    var npcnmb = npcinfo.nmb;
                    _RHTJ.reducePlayer(npc,npcnmb);
                }
            }

            _RHTJ.realRh(_RHTJ._ROLE,targetNpc);
            _RHTJ.closeUi();
            _TIPS.showMessage('融合成功!');
            _MRDL.refreshPage();
        }
    },

    realRh:function(role,npctype){
        
       //获取新角色类型
       var npcconfig = _NPC[npctype];
        
        //合并五维 取两者之高
        if(npcconfig.plv>role.plv){
            role.plv=npcconfig.plv;
        }
        if(npcconfig.ilv>role.ilv){
            role.ilv=npcconfig.ilv;
        }
        if(npcconfig.dlv>role.dlv){
            role.dlv=npcconfig.dlv;
        }
        if(npcconfig.mlv>role.mlv){
            role.mlv=npcconfig.mlv;
        }
        if(npcconfig.hlv>role.hlv){
            role.hlv=npcconfig.hlv;
        }


        //合并修为 取两者之高
        if(!role.xwlevel){
            role.xwlevel={};
            var oldnpcconfig = _NPC[role.npctype];
            for(var key in oldnpcconfig.npcxw){
                role.xwlevel[key]=oldnpcconfig.npcxw[key];
            }
        }
        for(var xw in npcconfig.npcxw){
            if(!role.xwlevel[xw]){
                role.xwlevel[xw]=npcconfig.npcxw[xw];
                role.xw[xw]=0;
            }else{
                if(
                    _RHM.getLevelToNmb(role.xwlevel[xw])<
                    _RHM.getLevelToNmb(npcconfig.npcxw[xw])
                ){
                    role.xwlevel[xw]=npcconfig.npcxw[xw];
                }
            }
        }

        //合并技能
        if(npcconfig.skill){
            for(var i=0;i<npcconfig.skill.length;i++){
                var skillname = npcconfig.skill[i];
                if(!_CU.isContain(role.skill,skillname)){
                    role.skill.push(skillname);
                }
            }
        }

        role.face = npcconfig.face;
        role.ftype = npcconfig.ftype;             
        role.pf = npcconfig.pf;
        role.npctype = npctype;

        _ROLEDATA.resetRoleQua(role);
    },

    reducePlayer:function(player,nmb){

        for(var i=window.gameData.playerTeamBack.length-1;i>=0;i--){
            var role = window.gameData.playerTeamBack[i];
            if(role.npctype==player){
                window.gameData.playerTeamBack.splice(i,1);
                nmb--;
                if(nmb==0){
                    return;
                }
            }
        }

    },

    canRh:function(){

        if(_RHTJ._TJRHOBJ){

            var rhdnmb = _RHTJ._TJRHOBJ.detail.rhnmb;
            var npcarr = _RHTJ._TJRHOBJ.detail.detail;
            if(_RHTJ.getrhdnmb()>=rhdnmb){

                for(var key in npcarr){
                    var npcinfo = npcarr[key][2];
                    if(npcinfo){
                        var npc = npcinfo.npc;
                        var npcnmb = npcinfo.nmb;
                        if(_RHTJ.getNpcNmb(npc)<npcnmb){
                            _TIPS.showMessage(npc+'不足无法融合');
                            return false;
                        }
                    }
                }

            }else{
                _TIPS.showMessage('融合丹不足无法融合');
                return false;
            }

            return true;

        }

        return false;
    }

}